package com.collection.web.shiro.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.collection.api.entity.sys.SysResource;
import com.collection.web.shiro.Menu;

public class TreeNodeUtil {
 
    public final static List<Menu> getfatherNode(List<SysResource> treeDataList) {
        List<Menu> newTreeDataList = new ArrayList<Menu>();
        for (SysResource resource : treeDataList) {
            if(resource != null ) {
                if(resource.getParentId().intValue() ==  0 ) {
                    
                    Menu menu =new Menu(resource.getId(),resource.getName(),StringUtils.replace(resource.getUrl(), "**", ""),resource.getSort(),null);
                    menu.setPid(resource.getParentId());
                    //获取父节点下的子节点
                    menu.setChildren(getChildrenNode(resource.getId(),treeDataList));
                    
                    newTreeDataList.add(menu);
                }
            }
        }
        return newTreeDataList;
    }
     
 
    private final static List<Menu> getChildrenNode(Integer pid , List<SysResource> treeDataList) {
        List<Menu> newTreeDataList = new ArrayList<Menu>();
        for (SysResource resource : treeDataList) {
            if(resource.getParentId().intValue()  ==  0)  continue;
            //这是一个子节点
            if(resource.getParentId().equals(pid)){
                
                Menu menu =new Menu(resource.getId(),resource.getName(),StringUtils.replace(resource.getUrl(), "**", ""),resource.getSort(),null);
                menu.setPid(resource.getParentId());
                //递归获取子节点下的子节点
                menu.setChildren(getChildrenNode(resource.getId() , treeDataList));
                newTreeDataList.add(menu);
            }
        }
        return newTreeDataList;
    }
}
